package com.lemon.smartsafety.http.service;

import android.content.Context;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.IApplication;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.news.ArticleInfoVo;
import com.lemon.smartsafety.http.UrlInfo;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafety.utils.common.AppUtils;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;
import com.lemon.safecloud.greendao.NewsColumn;
import com.lemon.safecloud.greendao.NewsColumnDao;
import com.zhy.http.okhttp.OkHttpUtils;

import java.util.ArrayList;
import java.util.List;

import static com.lemon.smartsafety.app.EventTag.TAG_MAIN_NEWS_COLUMN_HTTP;


/**
 * Created by Cooliean on 16/1/19.
 * 新闻
 */
public class NewsInfoServices extends BaseHttpServices {

    private LoggerHelper loggerHelper = new LoggerHelper(NewsInfoServices.class);

    private NewsInfoServices(Context context) {
        super(context);
    }

    public static NewsInfoServices init() {
        return new NewsInfoServices(IApplication.context());
    }


    /**
     * 获取新闻类型
     */
    public NewsInfoServices getNewsColumn() {
        getNewsColumn(new LemonRequestParams());
        return this;
    }


    /**
     * 获取新闻类型
     *
     * @param params
     */
    private NewsInfoServices getNewsColumn(LemonRequestParams params) {
        HandlerCallback<List<NewsColumn>> callback = new HandlerCallback<List<NewsColumn>>(TAG_MAIN_NEWS_COLUMN_HTTP) {
        };
        callback.localCallBack(getAllLocalNewsColumnInfo());
        if (!AppUtils.isNetworkAvailable(IApplication.context())) {//如果网络不正常,这里就获取本地
            return this;
        }
        callback.setInnerCallBack(new HandlerCallback.InnerCallBack<List<NewsColumn>>() {
            @Override
            public void onInnerCallBack(List<NewsColumn> data) {
                saveNewsColumnInfo(data);
            }
        });
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.URL_NEWS_COLUMN+"");
        OkHttpUtils.getInstance().post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }

    /**
     * 获取新闻类型的本地数据
     *
     * @return
     */
    public List<NewsColumn> getAllLocalNewsColumnInfo() {
        NewsColumnDao    dao    = AppService.getDBHelper().getDaoSession().getNewsColumnDao();
        List<NewsColumn> result = dao.queryBuilder().list();
        if (result == null) {
            return new ArrayList<NewsColumn>();
        }
        return result;
    }

    /**
     * 缓存新闻类型
     *
     * @param items
     * @return
     */
    public NewsInfoServices saveNewsColumnInfo(List<NewsColumn> items) {
        NewsColumnDao dao = AppService.getDBHelper().getDaoSession().getNewsColumnDao();
        try {
            if (items != null && !items.isEmpty()) {
                dao.insertOrReplaceInTx(items);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return this;
    }

    /**
     * 获取新闻列表
     *
     * @param type
     * @param pageSize
     * @param currentPage
     * @param keyword
     * @return
     */
    public NewsInfoServices getNewsList(RefreshType type, int pageSize, int currentPage, String columnId, String keyword) {
        LemonRequestParams params = new LemonRequestParams();
        params.put("page_size", pageSize + "");
        params.put("current_page", currentPage + "");
        params.put("keywords", StringUtils.nullValue(keyword));
        params.put("column_id", StringUtils.nullValue(columnId));
        return this.getNewsList(type, params);
    }

    /**
     * 获取新闻列表
     *
     * @param params
     * @return
     */
    private NewsInfoServices getNewsList(RefreshType type, LemonRequestParams params) {
        HandlerCallback<List<ArticleInfoVo>> callback = new HandlerCallback<List<ArticleInfoVo>>(EventTag.TAG_NEWS_LIST + params
                .get("column_id")) {

        };
        callback.setOther(type);
        String serverUrl = mSharedPreferencesServer.getRequestUrl(UrlInfo.ARTICLE_LIST);
        OkHttpUtils.post().url(serverUrl).tag(callback.getTag()).params(params).build().execute(callback);
        return this;
    }


}
